﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Ue.Qcdz.Repositories
{
    public class RelatingBase
    {
        private Relating relating;

        public RelatingBase(Relating relating)
        {
            this.relating = relating;
        }

        public bool IsHas(string leftId, string rightId)
        {
            string query = string.Format(
                "select count(*) from {0} where {1}=@{1} and {2}=@{2};",
                this.relating.RelatingTableName,
                this.relating.LeftTableRelatingFieldName,
                this.relating.RightTableRelatingFieldName);

            return SqlServerDriver.Instance.ExecuteScalar<bool>(
                query,
                SqlServerDriver.Instance.BuildParameter(this.relating.LeftTableRelatingFieldName, leftId),
                SqlServerDriver.Instance.BuildParameter(this.relating.RightTableRelatingFieldName, rightId));
        }

        public void AddRelating(string leftId, string rightId)
        {
            string insertQuery = string.Format(
                "insert into {0}({1},{2}) values(@{1},@{2});",
                this.relating.RelatingTableName,
                this.relating.LeftTableRelatingFieldName,
                this.relating.RightTableRelatingFieldName);

            SqlServerDriver.Instance.ExecuteNonQuery(
                insertQuery,
                SqlServerDriver.Instance.BuildParameter(this.relating.LeftTableRelatingFieldName, leftId),
                SqlServerDriver.Instance.BuildParameter(this.relating.RightTableRelatingFieldName, rightId));
        }

        public void DeleteRelating(string leftId, string rightId)
        {
            string query = string.Format(
                "delete {0} where {1}=@{1} and {2}=@{2};",
                this.relating.RelatingTableName,
                this.relating.LeftTableRelatingFieldName,
                this.relating.RightTableRelatingFieldName);

            SqlServerDriver.Instance.ExecuteNonQuery(
                query,
                SqlServerDriver.Instance.BuildParameter(this.relating.LeftTableRelatingFieldName, leftId),
                SqlServerDriver.Instance.BuildParameter(this.relating.RightTableRelatingFieldName, rightId));
        }

        public void DeleteRelatingByLeftId(string leftId)
        {
            this.DeleteRelatingBy(
                leftId, 
                this.relating.LeftTableRelatingFieldName);
        }

        public void DeleteRelatingByRightId(string rightId)
        {
            this.DeleteRelatingBy(
                rightId,
                this.relating.RightTableRelatingFieldName);
        }

        private void DeleteRelatingBy(string id, string identtiyFieldName)
        {
            string query = string.Format(
                "delete {0} where {1}=@{1};",
                this.relating.RelatingTableName,
                identtiyFieldName);

            SqlServerDriver.Instance.ExecuteNonQuery(
                query,
                SqlServerDriver.Instance.BuildParameter(identtiyFieldName, id));
        }
    }

    public class Relating
    {
        public Relating(string relatingTableName, string leftTableRelatingFieldName, string rightTableRelatingFieldName)
        {
            this.RelatingTableName = relatingTableName;
            this.LeftTableRelatingFieldName = leftTableRelatingFieldName;
            this.RightTableRelatingFieldName = rightTableRelatingFieldName;
        }

        public string RelatingTableName { get; private set; }

        public string LeftTableRelatingFieldName { get;private set; }

        public string RightTableRelatingFieldName { get;private set; }
    }
}
